using AutoMapper;
using Finance.Api.Write.Applications.Commands.库存调拨;
using Finance.Domain.调拨;
using Finance.ErrorCode;
using Finance.Infrastructure;
using MediatR;

namespace Finance.Api.Write.Applications.CommandHands.库存调拨
{
    public class DeploymentAddCommandHandler : IRequestHandler<DeploymentAddCommand, ApiResult<int>>
    {
        private readonly SqlsugarDb<DeploymentModel> db;
        private readonly SqlsugarDb<DeploymentDetail> dc;
        private readonly IMapper mapper;

        public DeploymentAddCommandHandler(SqlsugarDb<DeploymentModel> db, IMapper mapper, SqlsugarDb<DeploymentDetail> dc)
        {
            this.db = db;
            this.mapper = mapper;
            this.dc = dc;
        }

        public async Task<ApiResult<int>> Handle(DeploymentAddCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>();
            try
            {
                if (string.IsNullOrEmpty(request.Deployment))
                {
                    result.Code = Enumcode.Nonull;
                    result.message = "调拨单号不能为空";
                    return result;
                }

                if (string.IsNullOrEmpty(request.DeploymentStyle))
                {
                    result.Code = Enumcode.Nonull;
                    result.message = "调拨类型不能为空";
                    return result;
                }

                if (string.IsNullOrEmpty(request.DeploymentInto))
                {
                    result.Code = Enumcode.Nonull;
                    result.message = "调入仓库不能为空";
                    return result;
                }

                if (string.IsNullOrEmpty(request.DeploymentOut))
                {
                    result.Code = Enumcode.Nonull;
                    result.message = "调出仓库不能为空";
                    return result;
                }

                if (request.DetailList == null || request.DetailList.Count == 0)
                {
                    result.Code = Enumcode.Nonull;
                    result.message = "调拨明细不能为空";
                    return result;
                }

                var addResult = mapper.Map<DeploymentModel>(request);
                addResult.CreateTime = DateTime.Now;
                addResult.CreateName = "管理员"; // 固定值
                addResult.LoginId = 1; // 固定值
                addResult.IsDelete = false; // 确保IsDelete为false

                // 手动映射明细，不使用AutoMapper
                var detailEntity = new List<DeploymentDetail>();
                foreach (var detail in request.DetailList)
                {
                    var deploymentDetail = new DeploymentDetail
                    {
                        ProductNo = detail.ProductNo,
                        ProductName = detail.ProductName,
                        ProductNum = detail.ProductNum,
                        DeploymentNum = detail.DeploymentNum,
                        DeploymentMessage = detail.DeploymentMessage,
                        IsDelete = false
                    };
                    detailEntity.Add(deploymentDetail);
                }

                var rows = await db.BeginTransaction(async () =>
                {
                    var EntityId = await db.AddReturnId(addResult);
                    if (EntityId == 0)
                    {
                        result.message = "添加失败";
                        result.data = 0;
                        result.Code = Enumcode.fail;
                        return 0;
                    }

                    foreach (var item in detailEntity)
                    {
                        item.Deploymentid = EntityId;
                    }
                    var detailReturn = await dc.Addlist(detailEntity);
                    return detailReturn;
                });

                if (rows > 0)
                {
                    result.message = "添加成功";
                    result.data = 1;
                    result.Code = Enumcode.ok;
                }
                else
                {
                    result.message = "添加失败";
                    result.data = 0;
                    result.Code = Enumcode.fail;
                }
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
                result.data = 0;
                result.Code = Enumcode.fail;
            }

            return await Task.FromResult(result);
        }
    }
} 